<?php

class File {
	/**
     * [export_phpexcel xls导出模板]
     * @param  [type] $data     [description]
     * @return [type]           [description]
     */
    static function export_phpexcel($fileName = '',$headArr,$data = '') {
        require_once './PHPExcel/PHPExcel/IOFactory.php'; 
        require_once './PHPExcel/PHPExcel/Writer/Excel5.php';     // 用于其他低版本xls 
        require_once './PHPExcel/PHPExcel/Writer/Excel2007.php'; // 用于 excel-2007 格式 

        $date = date("Y_m_d",time());
        $d_time = time();
        $fileName .= "_{$date}_{$d_time}.xlsx";
     
        //创建新的PHPExcel对象
        $objPHPExcel = new PHPExcel();
        $objProps = $objPHPExcel->getProperties();
         
        //设置表头
        $key = ord("A");
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum.'1', $v);
            $key += 1;
        }
        //没有具体数据则不导出
        if ($data != '') {
            $column = 2;
            $objActSheet = $objPHPExcel->getActiveSheet();
            foreach($data as $key => $rows){ //行写入
                $span = ord("A");
                foreach($rows as $keyName=>$value){// 列写入
                    $j = chr($span);
                    $objActSheet->setCellValue($j.$column, $value);
                    $span++;
                }
                $column++;
            }
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);
        //重命名表
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        //将输出重定向到一个客户端web浏览器(Excel2007)
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        // if(!empty($_GET['excel'])){
            header('Content-Type: applicatiovnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header("Content-Disposition: attachment; filename=\"$fileName\"");
            header('Cache-Control: max-age=0');
          	$objWriter->save('php://output'); //文件通过浏览器下载
          
        // }else{
        //   $objWriter->save('./download/'.$fileName); //脚本方式运行，保存在当前目录
        // }
      return true;
    }


    public function export_excel($filename,$data) {  
    	header('Content-Type: application/vnd.ms-excel;charset=utf-8'); 
        // header("Content-type:text/csv");   
        header("Content-Disposition:attachment;filename=".$filename);   
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');   
        header('Expires:0');   
        header('Pragma:public');   
        echo $data;   
    }
}


?>